/*

BlackRabbit - OpenSource IDE.
@author Edward Sarkisyan (edward.sarkisyan@gmail.com)
Use LGPL (http://www.gnu.org/copyleft/lesser.html)

*/

#include "gadgetwidget.h"
#include "ui_gadgetwidget.h"

GadgetWidget::GadgetWidget(QWidget *parent, PluginsView *pluginView) :
    QWidget(parent),
    m_ui(new Ui::GadgetWidget)
{
    for(int i=0; i<6; i++)
        gadgets[i] = 0;
    m_ui->setupUi(this);
    numbersIsHidden = false;
    setPluginView(pluginView);
}

void GadgetWidget::setPluginView(PluginsView* pluginView)
{
    if(pluginView!=0)
    {
        plugins = pluginView;
        m_ui->gadget1Bttn->setEnabled(true);
        m_ui->gadget2Bttn->setEnabled(true);
        m_ui->gadget3Bttn->setEnabled(true);
        m_ui->gadget4Bttn->setEnabled(true);
        m_ui->gadget5Bttn->setEnabled(true);
        m_ui->gadget6Bttn->setEnabled(true);
    }
    else
    {
        m_ui->gadget1Bttn->setEnabled(false);
        m_ui->gadget2Bttn->setEnabled(false);
        m_ui->gadget3Bttn->setEnabled(false);
        m_ui->gadget4Bttn->setEnabled(false);
        m_ui->gadget5Bttn->setEnabled(false);
        m_ui->gadget6Bttn->setEnabled(false);
    }
}

GadgetWidget::~GadgetWidget()
{
    for(int i=0; i<6;i++)
        if(gadgets[i] !=0)
            delete gadgets[i];
    delete m_ui;
}

void GadgetWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void GadgetWidget::addPluginWidget(int which)
{
    qDebug()<<which;
    if(which>0 && which<7)
    {
        which--;
        if(gadgets[which]!=0)
            delete gadgets[which];
        gadgets[which] = new GadgetWindowWidget(this);
        Plugin* currentPlugin = plugins->getPlugin();
        bool showButton = true;
        if(currentPlugin!=0)
        {
            gadgets[which]->setWindowWidget(currentPlugin->widgetFor(WIDGET));
            gadgets[which]->setTitle(currentPlugin->name());
            gadgets[which]->setIcon(currentPlugin->icon(20, 20));
            if(currentPlugin->settings()==0)
                gadgets[which]->settingsButton()->setEnabled(false);
            else
            {
                gadgets[which]->settingsButton()->setEnabled(true);
                connect(gadgets[which]->settingsButton(), SIGNAL(clicked()), currentPlugin->settings(), SLOT(show()));
                qDebug("hm.,,");
            }
            int i=0, j=0;
            switch(which)
            {
                case 0:
                {
                    i=j=0;
                }
                break;
                case 1:
                {
                    i=0;
                    j=1;
                }
                break;
                case 2:
                {
                    i=1;
                    j=0;
                }
                break;
                case 3:
                {
                    i=j=1;
                }
                break;
                case 4:
                {
                    i=2;
                    j=0;
                }
                break;
                case 5:
                {
                    i=2;
                    j=1;
                }
                break;
                default:
                {
                    i=j=0;
                }
                break;
            }
            m_ui->gridLayout->addWidget(gadgets[which],i,j);
            showButton = false;
        }
        else
        {
            showButton = true;
        }
        switch(which)
        {
            case 0:
            {
                m_ui->gadget1Bttn->setVisible(showButton);

            }
            break;
            case 1:
            {
                m_ui->gadget2Bttn->setVisible(showButton);
            }
            break;
            case 2:
            {
                m_ui->gadget3Bttn->setVisible(showButton);
            }
            break;
            case 3:
            {
                m_ui->gadget4Bttn->setVisible(showButton);
            }
            break;
            case 4:
            {
                m_ui->gadget5Bttn->setVisible(showButton);
            }
            break;
            case 5:
            {
                m_ui->gadget6Bttn->setVisible(showButton);
            }
            break;
            default:
            {
            }
            break;
        }
    }
}

void GadgetWidget::on_hideNumbers_clicked()
{
    m_ui->gadget1Bttn->setVisible(numbersIsHidden);
    m_ui->gadget2Bttn->setVisible(numbersIsHidden);
    m_ui->gadget3Bttn->setVisible(numbersIsHidden);
    m_ui->gadget4Bttn->setVisible(numbersIsHidden);
    m_ui->gadget5Bttn->setVisible(numbersIsHidden);
    m_ui->gadget6Bttn->setVisible(numbersIsHidden);
    numbersIsHidden = !numbersIsHidden;
}

void GadgetWidget::on_gadget1Bttn_clicked()
{
    addPluginWidget(1);
}

void GadgetWidget::on_gadget2Bttn_clicked()
{
    addPluginWidget(2);
}

void GadgetWidget::on_gadget3Bttn_clicked()
{
    addPluginWidget(3);
}

void GadgetWidget::on_gadget4Bttn_clicked()
{
    addPluginWidget(4);
}

void GadgetWidget::on_gadget5Bttn_clicked()
{
    addPluginWidget(5);
}

void GadgetWidget::on_gadget6Bttn_clicked()
{
    addPluginWidget(6);
}
